package ln.frame.prac.microprofile.config.source;

import lombok.extern.slf4j.Slf4j;

import java.io.InputStream;
import java.net.URL;
import java.util.Map;
import java.util.Properties;

@Slf4j
public class DefaultResourceConfigSource extends MapBasedConfigSource {

    private static final String CONFIG_FILE_LOCATION = "META-INF/microprofile-config.properties";

    public DefaultResourceConfigSource() {
        super("Default Config File", 100);
    }

    @Override
    protected void prepareConfigData(Map configData) throws Throwable {
        ClassLoader classLoader = getClass().getClassLoader();
        URL resource = classLoader.getResource(CONFIG_FILE_LOCATION);
        if (resource == null) {
            log.error("The default config file cannot be found in the classpath: {}", CONFIG_FILE_LOCATION);
            return;
        }
        try (InputStream inputStream = resource.openStream()) {
            Properties properties = new Properties();
            properties.load(inputStream);
            configData.putAll(properties);
        }
    }
}
